<!-- 页面  -->
<template>
  <s-layout title="团队总业绩" :class="state.scrollTop ? 'team-wrap' : ''" navbar="inner">
    <view
      class="header-box"
      :style="[
        {
          marginTop: '-' + Number(statusBarHeight + 88) + 'rpx',
          paddingTop: Number(statusBarHeight + 108) + 'rpx',
        },
      ]"
    >
      <!-- 推荐人 -->
      <view v-if="userInfo.parent_user" class="referrer-box ss-flex ss-col-center">
        推荐人：
        <image
          class="referrer-avatar ss-m-r-10"
          :src="sheep.$url.cdn(state.parent_user.id ? state.parent_user.avatar : userInfo.parent_user.avatar)"
          mode="aspectFill"
        >
        </image>
        {{ state.parent_user.id ? state.parent_user.nickname : userInfo.parent_user.nickname }}
      </view>
      <!-- 团队数据总览 -->
      <view class="team-data-box">
        <view class="data-card">
          <view class="category-item ss-flex">
			  <view class="total-item ss-flex-1">
			    <view class="item-title">游戏总业绩</view>
			    <view class="total-num">{{ state.data.team_water || 0 }}</view>
			  </view>
			  <view class="total-item ss-flex-1" @click="goList">
			    <view class="item-title">游戏当日新增业绩</view>
			    <view class="total-num">{{ state.data.very_day || 0 }}</view>
			  </view>
		  </view>
        </view>
		
		<view class="data-card" style="margin-top: 20rpx;">
		  <view class="category-item ss-flex">
			  <view class="total-item ss-flex-1">
			    <view class="item-title">金积分总业绩</view>
			    <view class="total-num">{{ state.data.team_principal_water || 0 }}</view>
			  </view>
			  <view class="total-item ss-flex-1" @click="goJjfList">
			    <view class="item-title">金积分当日业绩</view>
			    <view class="total-num">{{ state.data.very_principal_day || 0 }}</view>
			  </view>
		  </view>
		</view>
        <!-- <view class="data-card">
          <view class="total-item">
            <view class="item-title">团队分销商人数（人）</view>
            <view class="total-num">{{ agentInfo.child_agent_count_all || 0 }}</view>
          </view>
          <view class="category-item ss-flex">
            <view class="ss-flex-1">
              <view class="item-title">一级分销商</view>
              <view class="category-num">{{ agentInfo.child_agent_count_1 || 0 }}</view>
            </view>
            <view class="ss-flex-1">
              <view class="item-title">二级分销商</view>
              <view class="category-num">{{ agentInfo.child_agent_count_2 || 0 }}</view>
            </view>
          </view>
        </view> -->
      </view>
    </view>
  </s-layout>
</template>

<script setup>
  import sheep from '@/sheep';
  import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  import { computed, reactive } from 'vue';
  import _ from 'lodash';
  import { onPageScroll } from '@dcloudio/uni-app';
import { useI18n } from 'vue-i18n'
  
  const { t }  = useI18n()
  const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
  const agentInfo = computed(() => sheep.$store('user').agentInfo);
  const userInfo = computed(() => sheep.$store('user').userInfo);
  const headerBg = sheep.$url.css('/assets/addons/shopro/uniapp/user/withdraw_bg.png');
  const sys_navBar = sheep.$platform.navbar;
  
  const tabMaps = [
	  {
		name: '第三天',
		value: '',
	  },
    {
      name: '近1天',
      value: '1',
    },
    {
      name: '近2天',
      value: '2',
    },
    {
      name:  '近三天',
      value: '3',
    },
  ];

  onPageScroll((e) => {
    if (e.scrollTop > 100) {
      state.scrollTop = false;
    } else {
      state.scrollTop = true;
    }
  });
  const pagination = {
    data: [],
    current_page: 1,
    total: 1,
    last_page: 1,
  };
  
  const state = reactive({
	  data: {},
		currentTab: 0,
		parent_user: {},
		pagination,
		loadStatus: '',
  });
  onLoad((options) => {
  	state.parent_user_id = options.parent_user_id
  });
  function onChange(e) {
	  state.pagination = pagination;
	  state.currentTab = e.index;
	  getTeamList();
  }

  function filterUserNum(num) {
    if (_.isNil(num)) {
      return '';
    }
    return `下级团队${num}人`;
  }
  
  async function getTeamData(page = 1, list_rows = 8) {
    state.loadStatus = 'loading';
	// console.log(state);
    let res = await sheep.$api.commission.team_yeji({
      list_rows,
      page,
  	  area: tabMaps[state.currentTab].value,
  	  parent_user_id: state.parent_user_id
    });
    if (res.code === 1) {
      state.data = res.data
	}
  }

  async function getTeamList(page = 1, list_rows = 8) {
    state.loadStatus = 'loading';
    let res = await sheep.$api.commission.team_yeji_list({
      list_rows,
      page,
  	  area: tabMaps[state.currentTab].value,
  	  parent_user_id: state.parent_user.id
    });
    if (res.code === 1) {
      let orderList = _.concat(state.pagination.data, res.data.data.data);
      state.pagination = {
        ...res.data,
        list: orderList,
      };
  	  console.log(state.pagination.data)
      if (state.pagination.data.current_page < state.pagination.data.last_page) {
        state.loadStatus = 'more';
      } else {
        state.loadStatus = 'noMore';
      }
    }
  }
  function goAllList() {
	  sheep.$router.go('/pages/commission/team-all-log')
  }
  function goJjfAllList() {
  	  sheep.$router.go('/pages/commission/jjfteamlog')
  }
  function goList() {
  	  sheep.$router.go('/pages/commission/team-log')
  }
  function goJjfList() {
  	  sheep.$router.go('/pages/commission/jjf-team-log')
  }
  function goTeam(item) {
	  if(item.parent_user_num > 0) {
		  sheep.$router.go('/pages/commission/team?user_id=' + item.id + '&avatar=' + item.avatar + '&nickname=' + item.nickname)
	  }
  }

	onLoad(async (option) => {
		if(option.user_id) {
			state.parent_user = {
				id: option.user_id,
				avatar: option.avatar,
				nickname: option.nickname,
			}
		}
		getTeamData();
		// getTeamList();
	});

  // 加载更多
  function loadmore() {
    if (state.loadStatus !== 'noMore') {
      getTeamList(state.pagination.current_page + 1);
    }
  }

  // 上拉加载更多
  onReachBottom(() => {
    loadmore();
  });
</script>

<style lang="scss" scoped>
  .header-box {
    box-sizing: border-box;
    padding: 0 20rpx 20rpx 20rpx;
    width: 750rpx;
    z-index: 3;
    position: relative;
    background: v-bind(headerBg) no-repeat,
      linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
    background-size: 750rpx 100%;
    // 团队信息总览
    .team-data-box {
      .data-card {
        width: 100%;
		box-sizing: border-box;
        background: #ffffff;
        border-radius: 20rpx;
        padding: 20rpx;

        .item-title {
          font-size: 22rpx;
          font-weight: 500;
          color: #999999;
          line-height: 30rpx;
          margin-bottom: 10rpx;
        }

        .total-item {
          padding: 20rpx 0;
        }

        .total-num {
          font-size: 38rpx;
          font-weight: 500;
          color: #333333;
          font-family: OPPOSANS;
        }

        .category-num {
          font-size: 26rpx;
          font-weight: 500;
          color: #333333;
          font-family: OPPOSANS;
        }
      }
    }
  }
  .list-box {
    z-index: 3;
    position: relative;
	
	.list-box-item {
		display: flex;
		align-items: center;
		padding: 20rpx;
		border-bottom: 1rpx solid #e5e5e5;
	}
	.item-avatar {
		min-width: 160rpx;
		width: 160rpx;
		height: 160rpx;
		border-radius: 160rpx;
		margin-right: 20rpx;
	}
	.item-right {
		flex: 1;
	}
	.item-nickname {
		padding-bottom: 10rpx;
		
		.nickname {
			font-size: 40rpx;
			font-weight: 600;
		}
		.level {
			color: #eb4938;
			margin-left: 20rpx;
		}
	}
	.item-item {
		padding: 5rpx 0;
		color: #666;
		
		.text-bold {
			font-size: 30rpx;
			font-weight: 600;
		}
	}
  }
  .chat-custom-right {
    .time-text {
      font-size: 22rpx;
      font-weight: 400;
      color: #999999;
    }

    .tag-box {
      background: rgba(0, 0, 0, 0.2);
      border-radius: 21rpx;
      line-height: 30rpx;
      padding: 5rpx 10rpx;
      width: 140rpx;

      .tag-img {
        width: 34rpx;
        height: 34rpx;
        margin-right: 6rpx;
        border-radius: 50%;
      }

      .tag-title {
        font-size: 18rpx;
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
        line-height: 20rpx;
      }
    }
  }

  // 推荐人
  .referrer-box {
    font-size: 28rpx;
    font-weight: 500;
    color: #ffffff;
    padding: 20rpx;

    .referrer-avatar {
      width: 34rpx;
      height: 34rpx;
      border-radius: 50%;
    }
  }
</style>
